/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package com.seagate.app.servlet;

import com.seagate.app.bean.MailGroupBean;
import com.seagate.app.manager.MailListManager;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 *
 * @author 452530
 */
public class MailListServlet extends HttpServlet {

    /**
     * Processes requests for both HTTP <code>GET</code> and <code>POST</code> methods.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
        response.setContentType("text/html;charset=UTF-8");
        response.setHeader("Cache-Control", "no-cache");
        response.setHeader("Pragma", "no-cache");
        PrintWriter out = response.getWriter();
        try {
             String action = request.getParameter("action");
          if("maillisttable".equalsIgnoreCase(action)){

          MailListManager lm=new MailListManager();
          List grouplist=lm.getMailGroupList();
          for(int j=0;j<grouplist.size();j++){
          MailGroupBean mgb=(MailGroupBean)grouplist.get(j);
          List maillist=lm.getMailListByGroupID(mgb.getMail_group_id());
          out.println("<br/>");
          out.println("<div>"+mgb.getMail_group_name()+"</div>");
          out.println("<table bgcolor=\"#CCCCCC\" cellpadding=\"0\" cellspacing=\"0\" border=\"0\">");
          out.println("<tr><td>");
          out.println("<table bgcolor=\"#6699CC\" cellpadding=\"3\" cellspacing=\"0\" border=\"0\" width=\"100%\">");

          out.println("<tr bgcolor=\"#eeeeee\">");
          out.println("<td>");
          out.println("<table bgcolor=\"#cccccc\" cellpadding=\"0\" cellspacing=\"0\" border=\"0\" width=\"100%\">");
          out.println("<tr><td>");
          out.println("<table bgcolor=\"#cccccc\" cellpadding=\"3\" cellspacing=\"1\" border=\"0\" width=\"100%\">");
            out.println("<tr bgcolor=\"#99CC66\">");

          out.println("<td nowrap><font color=\"#ffffff\">&nbsp;No&nbsp;</font></td>");
          out.println("<td nowrap><font color=\"#ffffff\">&nbsp;Email&nbsp;</font></td>");

          out.println("<td nowrap><font color=\"#ffffff\">&nbsp;ACTION&nbsp;</font></td>");


          out.println("</tr>");
          for(int i=0;i<maillist.size();i++){
              out.println("<tr bgcolor=\"#ffffff\">");
               out.println("<td>&nbsp;"+(i+1)+"&nbsp;</td>");
              out.println("<td>&nbsp;"+maillist.get(i)+"&nbsp;</td>");





              out.println("<td><input name=\"imgEdit\" id=\"imgEdit\" src=\"./images/buttons/btn_delete.gif\" onclick=\"deleteMail('"+maillist.get(i)+"')\"  border=\"0\"  type=\"image\"> </td>");

              out.println("</tr>");

           }

          out.println("</table>");
          out.println("</td></tr>");
          out.println("</table>");
          out.println("</td>");
          out.println("</tr>");

          out.println("</table>");
          out.println("</td></tr>");
          out.println("</table>");
          out.println("<br/>");
          }
          }else if("automaillisttable".equalsIgnoreCase(action)){

          MailListManager lm=new MailListManager();
          List maillist=lm.getAutoMailList();
          out.println("<table bgcolor=\"#CCCCCC\" cellpadding=\"0\" cellspacing=\"0\" border=\"0\">");
          out.println("<tr><td>");
          out.println("<table bgcolor=\"#6699CC\" cellpadding=\"3\" cellspacing=\"0\" border=\"0\" width=\"100%\">");

          out.println("<tr bgcolor=\"#eeeeee\">");
          out.println("<td>");
          out.println("<table bgcolor=\"#cccccc\" cellpadding=\"0\" cellspacing=\"0\" border=\"0\" width=\"100%\">");
          out.println("<tr><td>");
          out.println("<table bgcolor=\"#cccccc\" cellpadding=\"3\" cellspacing=\"1\" border=\"0\" width=\"100%\">");
            out.println("<tr bgcolor=\"#99CC66\">");

          out.println("<td nowrap><font color=\"#ffffff\">&nbsp;No&nbsp;</font></td>");
          out.println("<td nowrap><font color=\"#ffffff\">&nbsp;Email&nbsp;</font></td>");

          out.println("<td nowrap><font color=\"#ffffff\">&nbsp;ACTION&nbsp;</font></td>");


          out.println("</tr>");
          for(int i=0;i<maillist.size();i++){
              out.println("<tr bgcolor=\"#ffffff\">");
               out.println("<td>&nbsp;"+(i+1)+"&nbsp;</td>");
              out.println("<td>&nbsp;"+maillist.get(i)+"&nbsp;</td>");





              out.println("<td><input name=\"imgEdit\" id=\"imgEdit\" src=\"./images/buttons/btn_delete.gif\" onclick=\"deleteMail('"+maillist.get(i)+"')\"  border=\"0\"  type=\"image\"> </td>");

              out.println("</tr>");

           }

          out.println("</table>");
          out.println("</td></tr>");
          out.println("</table>");
          out.println("</td>");
          out.println("</tr>");

          out.println("</table>");
          out.println("</td></tr>");
          out.println("</table>");
          out.println("<br/>");

          }else if("maillistselect".equalsIgnoreCase(action)){
          String mailgroup = request.getParameter("mailgroup");
          MailListManager lm=new MailListManager();
          List maillist=lm.getMailListByGroupID(Integer.parseInt(mailgroup));


          for(int i=0;i<maillist.size();i++){
             String mailid= (String)maillist.get(i);


              out.println("<input type=\"checkbox\" name=\"sendlist\" value=\""+maillist.get(i)+"\" checked >"+mailid.replaceAll("@seagate.com", "")+"<br/>");

           }




          }else if("addMail".equalsIgnoreCase(action)){
                String mail = request.getParameter("mail");
                String mailgroup = request.getParameter("mailgroup");
                MailListManager lm=new MailListManager();
                boolean success=lm.addMail(mail,mailgroup);
                out.println("{\"success\":" + success + "}");
          }else if("addAutoMail".equalsIgnoreCase(action)){
                String mail = request.getParameter("mail");
               // String mailgroup = request.getParameter("mailgroup");
                MailListManager lm=new MailListManager();
                boolean success=lm.addAutoMail(mail);
                out.println("{\"success\":" + success + "}");
          }else if("delMail".equalsIgnoreCase(action)){
                String mail = request.getParameter("mail");
                MailListManager lm=new MailListManager();
                boolean success=lm.deleteMail(mail);
                out.println("{\"success\":" + success + "}");
          }else if("delAutoMail".equalsIgnoreCase(action)){
                String mail = request.getParameter("mail");
                MailListManager lm=new MailListManager();
                boolean success=lm.deleteAutoMail(mail);
                out.println("{\"success\":" + success + "}");
          }else if("checkMail".equalsIgnoreCase(action)){
                String mail = request.getParameter("mail");
                MailListManager lm=new MailListManager();
                boolean success=lm.checkMail(mail);
                out.println("{\"success\":" + success + "}");
          }else if("checkAutoMail".equalsIgnoreCase(action)){
                String mail = request.getParameter("mail");
                MailListManager lm=new MailListManager();
                boolean success=lm.checkAutoMail(mail);
                out.println("{\"success\":" + success + "}");
          }else if("mailgrouplist".equalsIgnoreCase(action)){
              MailListManager lm = new MailListManager();
              List grouplist = lm.getMailGroupList();
              
              for (int j = 0; j < grouplist.size(); j++) {
                  MailGroupBean mgb = (MailGroupBean) grouplist.get(j);
                  out.println("<option value=\""+mgb.getMail_group_id()+"\"  >"+mgb.getMail_group_name()+"</option>");

              }
          }
        } finally {
            out.close();
        }
    }

    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /**
     * Handles the HTTP <code>GET</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
        processRequest(request, response);
    }

    /**
     * Handles the HTTP <code>POST</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
        processRequest(request, response);
    }

    /**
     * Returns a short description of the servlet.
     * @return a String containing servlet description
     */
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>

}
